<!-- @Author: Yu_Bo -->
<template>
	<view class="placeorder">
		<!-- 地址 -->
		<view class="address" @click="addressBtn">
			<image class="left_icon" src="@/static/images/img/address.png" mode=""></image>
			<view class="list_info" v-if="address.address_id">
				<view class="info_top">
					<text class="name">{{address.name}} {{address.phone}}</text>
					<text class="tip_b" v-if="address.isschool == 0">学校</text>
				</view>
				<view class="info_bot line_one" v-if="address.isschool == 0">
					{{address.building}}
					{{address.chamber}}
				</view>
				<view class="info_bot line_one" v-if="address.isschool == 1">
					{{address.province}}
					{{address.city}}
					{{address.region}}
					{{address.detail}}
				</view>
			</view>
			<view class="list_text" v-else>
				点击添加收货地址
			</view>
			<image class="right_icon" src="@/static/images/img/right_icon.png" mode=""></image>
		</view>
		<!-- 书 -->
		<view class="placeorder_list">
			<view class="list" v-for="(item,index) in list" :key="index">
				<view class="img">
					<image :src="item.image" mode="aspectFill"></image>
				</view>
				<view class="info">
					<view class="title line_one">
						{{item.goods_name}}
					</view>
					<view class="txt line_one">
						{{item.author}}/{{item.press}}
					</view>
					<view class="tip line_one">
						{{item.label}}
					</view>
					<view class="sku">
						{{item.condition==1?'新书':'二手'}}
					</view>
					<view class="money">
						<view class="left">
							<text class="icon">￥</text>
							<text class="num">{{item.price}}</text>
							<text class="zhe color1" v-if="item.condition==0">二手{{item.discount}}折</text>
							<text class="zhe color2" v-if="item.condition==1">新书{{item.discount}}折</text>
						</view>
						<view class="right">
							x{{item.goods_num}}
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 总价 -->
		<view class="placeorder_box">
			<view class="left">
				总价
			</view>
			<view class="right">
				￥{{total_amount}}
			</view>
		</view>
		<!-- 运费 -->
		<view class="placeorder_box">
			<view class="left">
				运费
			</view>
			<view class="right">
				￥{{freight_price}}
			</view>
		</view>
		<!-- 备注 -->
		<view class="placeorder_text">
			<view class="title">
				备注
			</view>
			<view class="text">
				<u-input v-model="remarks" type="textarea" maxlength="50" height="200" :clearable="false"
					placeholder="若您有想对商家说的，可以备注在此。(选填)" :placeholder-style="placeholderStyle" />
			</view>
		</view>
		<!-- 底部按钮 -->
		<c-footer>
			<view class="book_footer">
				<view class="left">
					<text class="num">共{{total_num}}件</text>
					<text>合计：</text>
					￥{{allMoney}}
				</view>
				<view class="right">
					<view class="btn_txt color_right page_button" @click="submitBtn">
						<u-loading v-if="loading" mode="flower" color="#ffffff"></u-loading>
						提交订单
					</view>
				</view>
			</view>
		</c-footer>
	</view>
</template>

<script>
	export default {
		components: {},
		data() {
			return {
				id: "",
				address: {
					address_id: "",
					isschool: "",
					name: "",
					phone: "",
					province: "",
					city: "",
					region: "",
					detail: "",
					building: "",
					chamber: "",
				},
				list: [],
				total_amount: "0.00",
				total_num: "",
				// 
				freight_price: 0,
				// 
				remarks: "",
				// 
				loading: false,
			}
		},
		watch: {
			address(val) {
				this.getFreight()
			}
		},
		computed: {
			allMoney() {
				return (this.freight_price*100 + this.total_amount*100)/100
			}
		},
		onLoad(opt) {
			if(opt.id) {
				this.id = JSON.parse(opt.id).join(",")
			}
			this.getBook()
			this.getAddress()
		},
		onShow() {},
		methods: {
			// 获取书籍
			getBook() {
				var that = this
				var params = {
					cart_id: that.id
				}
				that.$httpApi.checkList(params).then((res) => {
					if (res.code == 1) {
						that.list = res.data.lists
						that.total_amount = res.data.total_amount
						that.total_num = res.data.total_num
					}
				})
			},
			// 获取地址数据
			getAddress() {
				var that = this
				that.$httpApi.addressList().then((res) => {
					if (res.code == 1) {
						var arr = res.data
						var index = arr.findIndex((item) => item.isdefault == 1)
						if (index !== -1) {
							this.address = {
								address_id: arr[index].address_id,
								isschool: arr[index].isschool,
								name: arr[index].name,
								phone: arr[index].phone,
								province: arr[index].Area?.province || "",
								city: arr[index].Area?.city || "",
								region: arr[index].Area?.region || "",
								detail: arr[index].detail || "",
								building: arr[index].building || "",
								chamber: arr[index].chamber || "",
							}
						}
					}
				})
			},
			// 选择地址
			addressBtn() {
				this.$navToLogin("/pagesWo/address/index?type=1")
			},
			// 获取运费
			getFreight() {
				var that = this
				var params = {
					address_id: that.address.address_id
				}
				that.$httpApi.freightPrice(params).then((res) => {
					if (res.code == 1) {
						that.freight_price = res.data.price
					}
				})
			},
			// 提交订单
			submitBtn() {
				var that = this
				if(that.address.address_id === "") {
					that.$tips.showToast('请添加收货地址')
					return
				}
				if(that.loading) return
				that.loading = true
				var params = {
					cart_id: that.id,
					address_id: that.address.address_id ,
					remarks: that.remarks,
				}
				that.$httpApi.submitOrder(params).then((res) => {
					that.loading = false
					if (res.code == 1) {
						that.getPlay(res.data.order_id)
					}
				})
			},
			// 调取支付
			getPlay(id) {
				this.$getOrder(id).then(res=>{
					setTimeout(()=>{
						uni.navigateBack()
					},1500)
				})
			},
		},
	}
</script>

<style lang='scss' scoped>
	.placeorder {
		width: 100%;
		min-height: 100vh;
		padding: 30rpx;
		background: #f6f6f6;

		.address {
			width: 100%;
			padding: 30rpx 20rpx;
			background: #ffffff;
			border-radius: 20rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.left_icon {
				display: block;
				width: 42rpx;
				height: 42rpx;
			}

			.list_text {
				width: 578rpx;
				padding: 0 20rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 30rpx;
				color: #5A68FF;
				display: flex;
				align-items: center;
			}

			.list_info {
				width: 578rpx;
				padding: 0 20rpx;

				.info_top {
					width: 100%;
					display: flex;
					align-items: center;

					.name {
						margin-right: 20rpx;
						font-family: PingFang SC, PingFang SC;
						font-weight: 500;
						font-size: 30rpx;
						color: #333333;
					}

					.tip_a {
						margin-left: 10rpx;
						font-family: PingFang SC, PingFang SC;
						font-weight: 400;
						font-size: 20rpx;
						color: #5A68FF;
						padding: 2rpx 8rpx;
						background: #E9EBFF;
						border-radius: 8rpx;
						border: 2rpx solid #5A68FF;
					}

					.tip_b {
						margin-left: 10rpx;
						font-family: PingFang SC, PingFang SC;
						font-weight: 400;
						font-size: 20rpx;
						color: #FF9400;
						padding: 2rpx 8rpx;
						background: #FFF4E4;
						border-radius: 8rpx;
						border: 2rpx solid #FF9400;
					}
				}

				.info_bot {
					width: 100%;
					padding-top: 10rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 26rpx;
					color: #999999;
				}
			}

			.right_icon {
				display: block;
				width: 30rpx;
				height: 30rpx;
			}
		}

		.placeorder_list {
			width: 100%;

			.list {
				width: 100%;
				padding: 20rpx;
				margin-top: 30rpx;
				background: #fff;
				border-radius: 20rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.img {
					width: 180rpx;
					height: 240rpx;

					image {
						display: block;
						width: 100%;
						height: 100%;
					}
				}

				.info {
					width: calc(100% - 180rpx);
					height: 240rpx;
					padding-left: 20rpx;
					display: flex;
					flex-direction: column;
					justify-content: space-between;

					.title {
						width: 100%;
						font-family: PingFang SC, PingFang SC;
						font-weight: 500;
						font-size: 32rpx;
						color: #222222;
					}

					.txt {
						width: 100%;
						font-family: PingFang SC, PingFang SC;
						font-weight: 400;
						font-size: 24rpx;
						color: #666666;
					}

					.tip {
						width: 100%;
						font-family: PingFang SC, PingFang SC;
						font-weight: 400;
						font-size: 24rpx;
						color: #00A8FF;
					}

					.sku {
						width: 100%;
						font-family: PingFang SC, PingFang SC;
						font-weight: 400;
						font-size: 24rpx;
						color: #222222;
					}

					.money {
						width: 100%;
						display: flex;
						align-items: center;
						justify-content: space-between;

						.left {
							display: flex;
							align-items: center;

							.icon {
								padding-top: 5rpx;
								font-family: PingFang SC, PingFang SC;
								font-weight: bold;
								font-size: 24rpx;
								color: #5A68FF;
							}

							.num {
								font-family: PingFang SC, PingFang SC;
								font-weight: bold;
								font-size: 36rpx;
								color: #5A68FF;
							}

							.zhe {
								margin-left: 30rpx;
								padding: 2rpx 10rpx;
								font-family: PingFang SC, PingFang SC;
								font-weight: 400;
								font-size: 22rpx;
								background: #fff;
								border-radius: 10rpx;
							}

							.color1 {
								color: #57CB82;
								border: 2rpx solid #57CB82;
							}

							.color2 {
								color: #FF9400;
								border: 2rpx solid #FF9400;
							}
						}

						.right {
							font-family: PingFang SC, PingFang SC;
							font-weight: 400;
							font-size: 28rpx;
							color: #666666;
						}
					}
				}
			}
		}

		.placeorder_box {
			width: 100%;
			margin-top: 30rpx;
			padding: 30rpx;
			background: #ffffff;
			border-radius: 20rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.left {
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 28rpx;
				color: #222222;
			}

			.right {
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 28rpx;
				color: #FF3E3E;
			}
		}

		.placeorder_text {
			width: 100%;
			margin-top: 30rpx;
			padding: 30rpx;
			background: #ffffff;
			border-radius: 20rpx;

			.title {
				width: 100%;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 28rpx;
				color: #222222;
			}

			.text {
				width: 100%;
				margin-top: 30rpx;
				padding: 20rpx 30rpx;
				background: #F6F6F6;
				border-radius: 20rpx;
			}
		}

		.book_footer {
			width: 100%;
			height: 100rpx;
			padding: 30rpx 30rpx;
			background: #fff;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.left {
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 28rpx;
				color: #FF3E3E;
				display: flex;
				align-items: center;

				text {
					font-family: PingFang SC, PingFang SC;
					font-weight: 500;
					font-size: 28rpx;
					color: #3D444D;
				}

				.num {
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 26rpx;
					color: #999999;
					margin-right: 10rpx;
				}
			}

			.right {
				display: flex;
				align-items: center;
				justify-content: flex-end;

				.btn_txt {
					width: 200rpx;
					height: 70rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 28rpx;
					color: #FFFFFF;
					border-radius: 45rpx;
					display: flex;
					align-items: center;
					justify-content: center;
				}
			}
		}
	}
</style>